package com.blb.examsystem.service.Impl;

import com.blb.common.entity.PageBean;
import com.blb.common.entity.Question;
import com.blb.examsystem.dao.QuestionDao;
import com.blb.examsystem.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class QuestionServiceImpl implements QuestionService {
    @Autowired
    private QuestionDao questionDao;

    @Override
    public PageBean<Question> findPage(int current) {
        int dataCount = questionDao.selectCount();
        int pageCount = dataCount % PageBean.PAGE_SIZE == 0 ? dataCount / PageBean.PAGE_SIZE : dataCount / PageBean.PAGE_SIZE +1;
        List<Question> questions = questionDao.selectLimit((current - 1) * PageBean.PAGE_SIZE, PageBean.PAGE_SIZE);
        PageBean<Question> pageBean = new PageBean<>();
        pageBean.setCurrent(current);
        pageBean.setData(questions);
        pageBean.setDataCount(dataCount);
        pageBean.setPageCount(pageCount);
        return pageBean;
    }

    @Override
    public PageBean<Question> findPageByContent(String content, int current) {
        int dataCount = questionDao.selectCountByContent(content);
        int pageCount = dataCount % PageBean.PAGE_SIZE == 0 ? dataCount / PageBean.PAGE_SIZE : dataCount / PageBean.PAGE_SIZE +1;
        List<Question> questions = questionDao.selectLimitByContent(content,(current - 1) * PageBean.PAGE_SIZE, PageBean.PAGE_SIZE);
        PageBean<Question> pageBean = new PageBean<>();
        pageBean.setCurrent(current);
        pageBean.setData(questions);
        pageBean.setDataCount(dataCount);
        pageBean.setPageCount(pageCount);
        return pageBean;
    }

    @Override
    public int addQuestion(Question question) {
        return questionDao.insertQuestion(question);
    }

    @Override
    public Question findById(int id) {
        List<Question> questions = questionDao.selectById(id);
        if (questions != null && questions.size()>0){
            return questions.get(0);
        }
        return null;
    }

    @Override
    public int modifyQuestion(Question question) {
        return questionDao.updateQuestion(question);
    }

    @Override
    public int removeQuestion(int id) {
        return questionDao.deleteQuestion(id);
    }
}
